﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using hcom;
using System.Text;

namespace hcms.Admin
{
    public partial class tree : System.Web.UI.Page
    {
		public StringBuilder treedata = new StringBuilder(10000);
		public Model model = Global.model;
		protected void Page_Load(object sender, EventArgs e)
		{
			username.InnerText = model.getNoteString("Username");
			inittree();
		}


		/// <summary>
		/// 初始化栏目树，递归调用。
		/// </summary>
		/// <param name="rows">表行</param>
		/// <param name="i">起始位置行</param>
		private int initcolumn(DataRowCollection rows, int i)
		{
			// 检查起始行
			System.Diagnostics.Debug.Assert(i < rows.Count);

			/*DataRow row = rows[i];
			int len = row["colID"].ToString().Length;	// 当前级别的栏目id长度
			int ll = len;								// 当前行的栏目id长度
			int count = rows.Count;
			treedata.AppendFormat("[{{'title':'{0}', 'column':true, 'key':'{1}', isFolder:true", row["colName"], row["colID"]);	// json数组, 两个大括号表示一个
			for (i++; i < count && len == ll; i++)
			{
				row = rows[i];
				ll = row["colID"].ToString().Length;

				if (ll == len)	// 栏目号长度相等，加一项同级内容。
				{
					treedata.AppendFormat("}},{{'title':'{0}', 'column':true, 'key':'{1}', isFolder:true", row["colName"], row["colID"]);
				}
				else if (ll > len)	// 栏目号变长，添加子节点内容
				{
					treedata.Append(", children: ");
					i = initcolumn(rows, i);
				}
			}
			treedata.Append("}]");

			return i;*/

			DataRow row = rows[i];
			int len = row["colID"].ToString().Length;	// 当前级别的栏目id长度
			int ll = len;								// 当前行的栏目id长度
			int count = rows.Count;
			treedata.AppendFormat("[{{'title':'{0}', 'column':true, 'key':'{1}', isFolder:true", row["colName"], row["colID"]);	// json数组, 两个大括号表示一个
			for (i++; i < count; )
			{
				row = rows[i];
				ll = row["colID"].ToString().Length;

				if (ll == len)	// 栏目号长度相等，加一项同级内容。
				{
					treedata.AppendFormat("}},{{'title':'{0}', 'column':true, 'key':'{1}', isFolder:true", row["colName"], row["colID"]);
					i++;
				}
				else if (ll > len)	// 栏目号变长，添加子节点内容
				{
					treedata.Append(", children: ");
					i = initcolumn(rows, i);
				}
				else
					break;
			}
			treedata.Append("}]");

			return i;

		}

		/// <summary>
		/// 初始化树形控件，使用dynatree插件, 使用说明详见 \参考\dynatree-1.2.0\index.html，http://code.google.com/p/dynatree/
		/// </summary>
		private void inittree()
		{
			treedata.Append("[{title:'常规', isFolder:true, expand:true, hideCheckbox: true, children: [" +
				"{title:'网站首页', hideCheckbox: true, url:'/index.htm', urlopen:true}," +
				"{title:'访问量统计', hideCheckbox: true}," +
				"{title:'文档查询', hideCheckbox: true}," + 
				"{title:'更新网站', hideCheckbox: true, url:'/Admin/UpdateSite.aspx'}" +
				"]}," +

				"{title:'栏目管理', hideCheckbox: true, 'url':'ColumnList.aspx', isFolder:true, expand:true, children: ");

			DataRowCollection rows = model.getColumnRows(null, true);	// 取全部栏目，包括子栏目
			initcolumn( rows, 0 );

			treedata.Append("}, {title:'模板管理', hideCheckbox: true, isFolder:true, expand:true, children: [" +	// 第一个大括号对应 “{title:'栏目管理'”的大括号
				"{title:'栏目模板', hideCheckbox: true, isFolder:true, url:'/Admin/TemplateList.aspx?type=t'}," +
				"{title:'文档模板', hideCheckbox: true, isFolder:true, url:'/Admin/TemplateList.aspx?type=a'}," +
				"{title:'板块管理', hideCheckbox: true, isFolder:true, url:'/Admin/TemplateList.aspx?type=d'}," +
				// ??考虑合并到板块管理中，是不是要清除数据库？ "{title:'静态板块', isFolder:true, url:'/Admin/TemplateList.aspx?type=静态板块'}," +ft
				"{title:'编辑首页', hideCheckbox: true, url:'/Admin/HomeTemplate.aspx'}" + 
				"]}," +

				"{title:'系统管理', hideCheckbox: true, isFolder:true, expand:true, children: [" +
				"{title:'重定向', hideCheckbox: true, url:'/Admin/RedirectList.aspx'}," +
				"{title:'系统选项', hideCheckbox: true, url:'/Admin/Options.aspx'}," +
				"{title:'用户管理', hideCheckbox: true, url:'/Admin/UserList.aspx'}," +
				"{title:'角色管理', hideCheckbox: true}" +
				"]}," +

				"{title:'技术支持', hideCheckbox: true, isFolder:true, expand:true, children: [" +
				"{title:'快速入门', hideCheckbox: true, url:'/Help/beginner.htm'}," +
				"{title:'板块操作说明', hideCheckbox: true, url:'/Help/beginner.htm'}," +
				"{title:'模板操作说明', hideCheckbox: true, url:'/Help/beginner.htm'}," +
				"{title:'查看日志', hideCheckbox: true, url:'/Admin/Loges.htm'}" +
				"]}," +

				"{title:'回收站', hideCheckbox: true, isFolder:true, children: [" +
				"{title:'删除的栏目', hideCheckbox: true}," +
				"{title:'删除的文档', hideCheckbox: true}," +
				"{title:'删除的模板', hideCheckbox: true}," +
				"{title:'无栏目文档', hideCheckbox: true}" +
				"]}]"
				);
		}
	}
}